package com.uzai.console.service.alipaymch.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.alipaymchbatch.AlipayMchBatchQuery;
import com.uzai.console.entity.AlipayMchBatch;
import com.uzai.console.enums.AlipayMchBatchStatusEnum;
import com.uzai.console.mapper.AlipayMchBatchMapper;
import com.uzai.console.service.alipaymch.AlipayMchBatchService;
import com.uzai.console.vo.alipaymchbatch.AlipayMchBatchVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Batcher: lucher
 * @Date: 2021/12/14 16:46
 * @Description:
 */
@Service
public class AlipayMchBatchServiceImpl implements AlipayMchBatchService {

    private static Logger logger = LoggerFactory.getLogger(AlipayMchBatchServiceImpl.class);

    @Autowired
    private AlipayMchBatchMapper alipayMchBatchMapper;
    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    /**
     * 查询支付宝个人批量转账列表
     * @batchor liuqi
     * @date 2021/8/13 11:38
     * @param alipayMchBatchQuery
     * @return null
     */
    public Page<AlipayMchBatchVo> findAlipayMchBatchList(AlipayMchBatchQuery alipayMchBatchQuery){
        //新建返回到页面的LIST对象
        List<AlipayMchBatchVo> alipayMchBatchVoList = new ArrayList<>();

        //执行分页查询对象
        Page<AlipayMchBatch> page = new Page<>();
        alipayMchBatchQuery.convert(page);
        ArrayList<AlipayMchBatch> alipayMchBatchList =  alipayMchBatchMapper.findByList(page, alipayMchBatchQuery);
        if(alipayMchBatchList != null && alipayMchBatchList.size() > 0){
            for (AlipayMchBatch alipayMchBatch : alipayMchBatchList){
                AlipayMchBatchVo alipayMchBatchVo = new AlipayMchBatchVo();
                BeanUtils.copyProperties(alipayMchBatch, alipayMchBatchVo);

                try {
                    //成功金额
                    Double successAmount = 0.00D;
                    if(StringUtils.isNotBlank(alipayMchBatch.getSuccessAmount())){
                        successAmount = Double.parseDouble(alipayMchBatch.getSuccessAmount());
                    }
                    successAmount = Tools.getDouble(successAmount);
                    alipayMchBatchVo.setSuccessAmount(successAmount);

                    //成功笔数
                    Integer successCount = Tools.getInteger(alipayMchBatch.getSuccessCount());
                    alipayMchBatchVo.setSuccessCount(successCount);

                    //总金额
                    Double totalAmount = Tools.getDouble(alipayMchBatch.getTotalAmount());
                    //失败金额=总金额-成功成功
                    Double failAmount = com.uzai.common.utils.Math.sub(totalAmount, successAmount);
                    alipayMchBatchVo.setFailAmount(failAmount);

                    //总笔数
                    Integer totalCount = Tools.getInteger(alipayMchBatch.getTotalItemCount());
                    //失败笔数=总笔数-成功笔数
                    Integer failCount = totalCount.intValue() - successCount.intValue();
                    alipayMchBatchVo.setFailCount(failCount);

                    if(StringUtils.isNotBlank(alipayMchBatch.getBatchStatus())){
                        AlipayMchBatchStatusEnum alipayMchBatchStatusEnum = AlipayMchBatchStatusEnum.getByKey(alipayMchBatch.getBatchStatus());
                        if(alipayMchBatchStatusEnum != null){
                            alipayMchBatchVo.setStatusName(alipayMchBatchStatusEnum.getDesc());
                        }
                    }
                }catch (Exception e){

                }


                alipayMchBatchVoList.add(alipayMchBatchVo);
            }
        }

        //返回到页面分页对象
        Page<AlipayMchBatchVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(alipayMchBatchVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

}
